package com.hwadee.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwadee.property.mapper.UserAcitivityLogMapper;
import com.hwadee.property.pojo.UserActivityLog;
import com.hwadee.property.service.IUserActivityLogService;
import com.hwadee.property.util.DateUtil;
import com.hwadee.property.util.ErrorCodes;
import com.hwadee.property.dto.PageResult;
import com.hwadee.property.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户活动日志服务实现类
 */
@Service
public class UserActivityLogServiceImpl extends ServiceImpl<UserAcitivityLogMapper, UserActivityLog> implements IUserActivityLogService {

    @Autowired
    private UserAcitivityLogMapper activityLogMapper;

    @Override
    public PageResult<UserActivityLog> getActivityLogsByPage(Long userId, Long communityId, Integer userType,
                                                             String logType, String startTime, String endTime,
                                                             int currentPage, int pageSize) {
        if (currentPage <= 0 || pageSize <= 0) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "当前页和页大小必须大于零");
        }

        int offset = (currentPage - 1) * pageSize;

        List<UserActivityLog> records = activityLogMapper.getActivityLogsByPage(
                userId, communityId, userType, logType, startTime, endTime, offset, pageSize
        );
        int total = activityLogMapper.countActivityLogs(
                userId, communityId, userType, logType, startTime, endTime
        );

        if (total == 0) {
            throw new BusinessException(ErrorCodes.NOT_FOUND, "未找到符合条件的活动日志");
        }

        return new PageResult<>(total, currentPage, pageSize, records);
    }


    @Override
    public List<UserActivityLog> getActivityLogsByUserIdAndTimeRange(Long userId, String startTime, String endTime) {
        // 校验参数合法性
        if (userId == null || startTime == null || endTime == null) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "用户ID或时间范围不能为空");
        }

        // 使用 QueryWrapper 构建查询条件
        QueryWrapper<UserActivityLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .ge("activity_time", startTime)
                .le("activity_time", endTime);

        List<UserActivityLog> logs = this.list(queryWrapper);
        if (logs.isEmpty()) {
            throw new BusinessException(ErrorCodes.NOT_FOUND, "没有找到该用户在指定时间范围内的活动日志");
        }
        return logs;
    }

    @Override
    public List<UserActivityLog> getActivityLogsByCommunityId(Long communityId) {
        // 校验参数合法性
        if (communityId == null) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "小区ID不能为空");
        }

        // 使用 QueryWrapper 构建查询条件
        QueryWrapper<UserActivityLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("community_id", communityId);

        List<UserActivityLog> logs = this.list(queryWrapper);
        if (logs.isEmpty()) {
            throw new BusinessException(ErrorCodes.NOT_FOUND, "没有找到该小区的活动日志");
        }
        return logs;
    }

    @Override
    public List<UserActivityLog> getActivityLogsByUserId(Long userId) {
        // 校验参数合法性
        if (userId == null) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "用户ID不能为空");
        }

        // 使用 QueryWrapper 构建查询条件
        QueryWrapper<UserActivityLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);

        List<UserActivityLog> logs = this.list(queryWrapper);
        if (logs.isEmpty()) {
            throw new BusinessException(ErrorCodes.NOT_FOUND, "没有找到该用户的活动日志");
        }
        return logs;
    }

    @Override
    public boolean saveActivityLog(Long userID, Long communityID, String content, Double amount, String logType, String relatedRecordID) {
        UserActivityLog userActivityLog = new UserActivityLog();
        userActivityLog.setUserId(userID);
        userActivityLog.setCommunityId(communityID);
        userActivityLog.setLogType(logType);
        userActivityLog.setActivityDetail(content);
        userActivityLog.setAmount(amount);
        userActivityLog.setActivityTime(DateUtil.dateToString(new Date()));
        userActivityLog.setRelatedRecordId(relatedRecordID);

        // 保存日志
        return this.save(userActivityLog);
    }

    @Override
    public List<UserActivityLog> getActivityLogsByTimeRange(String startTime, String endTime) {
        // 使用 QueryWrapper 构建查询条件
        QueryWrapper<UserActivityLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("activity_time", startTime).le("activity_time", endTime);

        List<UserActivityLog> logs = this.list(queryWrapper);
        if (logs.isEmpty()) {
            throw new BusinessException(ErrorCodes.NOT_FOUND, "没有找到在指定时间范围内的活动日志");
        }
        return logs;
    }

}
